<template>
  <work-template v-model="detailForm" @submitOn="submitOn" :flowId="flowId"
                 @closeSelectedTag="closeSelectedTag" :butShow="bool">
    <div slot="form" class="container-div">
      <tire-basice-vehicle v-model="detailForm" :state="isEdit" ref="refTireBasiceVehicle"/>
      <tire-basice-info v-model="detailForm" ref="refTireDetailForm" :state="isEdit"/>
      <replacement-details v-model="detailForm" ref="refReplacement" :state="isEdit"/>
    </div>
  </work-template>
</template>

<script>
import {
  getToken
} from "@/utils/auth";
import {
  updateReplaceTyre, addReplaceTyre, getReplaceTyre
} from "@/api/cwgd/replaceTireApi.js";
import {
  getWorkOrder, carBasic
} from "@/api/cwgd/cwgdApi.js";
import {getFindCarWorkOrderInfoVo} from "@/api/cwgd/maintenanceApi.js";

import LinkageSelect from "../../components/linkageSelect";
import tireBasiceVehicle from "../components/changeTir/tireBasiceVehicle.vue"
import tireBasiceInfo from "../components/changeTir/tireBasiceInfo.vue"
import replacementDetails from "../components/changeTir/replacementDetails.vue"

export default {
  dicts: ['work_order_type', 'online_offline', 'maintain_type'],
  name: "ReplaceTireDetails",
  components: {
    LinkageSelect, tireBasiceVehicle, tireBasiceInfo, replacementDetails

  },

  data() {
    return {

      isForm: true,
      method: {},
      // 表单参数
      detailForm: {
        vinNo: "",
        carNo: "",
        brandName: "",
        seriesName: "",
        modelName: "",
        belongCityName: "",
        orderNo: "",
        leaseNum: "",
        customerName: "",
        leaseStartTime: "",
        leaseEndTime: "",
        totalBudgetPrice: "",
        registerTime: "",
        bicycleBudgetPrice: "",
        replacePrice: "",
        carSurplusPrice: "",
        lastReplaceTime: "",

        workId: '',
        expectStartTime: "",
        expectEndTime: "",
        maintenanceShop: "",
        maintenanceShopId:"",
        faultPhenomenon: "",
        maintenanceFaultDiagnosis: "",
        maintenanceQuoter: "",
        reasonForClaim: "",
        claimantReason: "",
        swappingAccessories: "",
        comprehensiveRecommendations: "",
        normalRemark: "",
        carId: "",
        oldTyreMeligile: "",
        isReplaceSpareTire: "",
        tyreBrand: "",
        tyreModel: "",
        tyreFigure: "",
        leadSpeedLevel: "",
        tyreNum: "",
        travelMeligile: '',
        totalPrice: "",
        tyreRemark: "",

        //更换轮胎反馈
        actualStartTime: "",
        actualEndTime: "",
        feedbackAdvancePayment: "",
        payType: "",
        replaceCompleteUrl: "",
        balanceUrl: "",
        replaceFeedback: "",
        annexUrl: "",
      },

      addAmountNun: 0,
      state: this.$route.query.type
    };
  },
  watch: {},
  computed: {
    isEdit() {
      if (this.$route.query.type === "edit") {
        return "write";
      }
      if (this.$route.query.type === "view") {
        return "readonly";
      }
    },
    bool() {
      if (this.$route.query.type === "edit") {
        return true;
      }
      if (this.$route.query.type === "view") {
        return false;
      }
    },
    flowId() {
      if (this.$route.query.flowId) {
        return this.$route.query.flowId
      }
    }

  },
  created() {
    this.method = this.$route.query
    this.detailForm.carId = this.$route.query.carId
    this.handleGetFindCarWorkOrderInfoVo()
    if (this.$route.query.businessDataId) {
      this.getReplaceTyre();
    }
  },
  mounted() {

  },
  methods: {

    handleGetFindCarWorkOrderInfoVo() {
      getFindCarWorkOrderInfoVo(this.$route.query.id).then(res => {
        if (res.code === 200) {
          for (let key in res.data) {
            this.$set(this.detailForm, key, res.data[key]);
          }
          this.detailForm.workId  = res.data?.workId || this.$route.query.id
        }
      })
    },

    closeSelectedTag(view) {
      this.$tab.closePage(view).then(({visitedViews}) => {
        this.$router.push({
          path: "/cwgd/replaceTireList",
        })
      })
    },



    // 提交操作
    submitOn() {
      let arr = [
        this.$refs.refTireBasiceVehicle.handleEffect(),
        this.$refs.refTireDetailForm.handleEffect(),
        this.$refs.refReplacement.handleEffect()
      ]
      Promise.all(arr).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '保存中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        this.detailForm.workId = this.method.id

        addReplaceTyre(this.detailForm).then((res) => {
          if (res.code === 200) {
            loading.close();
            this.closeSelectedTag(this.$route)
          }
        }).catch(() => {
          loading.close();
        })

      }).catch(() => {
      })
    },

    // 详情回显
    getReplaceTyre() {
      let id = this.method.businessDataId

      getReplaceTyre(id).then(res => {
        if (res.code === 200) {
          this.isForm = true
          this.detailForm = res.data
          this.detailForm.workId  = this.$route.query.id
        }
      })
    }
  }
};
</script>


<style lang="scss" scoped>

@import "@/styles/cwgd/container.scss";
</style>
